package indi.kider.server.applet.api;


import indi.kider.server.applet.model.User;
import indi.kider.server.applet.service.CommService;
import indi.kider.server.applet.service.PayBillService;
import indi.kider.server.applet.service.UserService;
import indi.kider.server.employee.model.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;

@Slf4j
@RestController
@RequestMapping(value = "user", produces = "application/json;charset=utf8")
public class UserController {

    @Autowired
    private UserService    userService;
    @Autowired
    private CommService    commService;
    @Autowired
    private PayBillService payBillService;

    @PostMapping("app-user")
    public CommonResponse<AppUser> login(@RequestBody User user) {
        try {
            AppUser au = userService.login(user);
            log.info("【au{}】", au);
            return CommonResponse.buildGetSuccessResponse(au);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/update")
    public CommonResponse<String> updatePwd(@RequestBody User user, HttpServletRequest request) {
        try {
            User u = commService.getCurrentUser(request, user);
            return CommonResponse.buildUpdateSuccessResponse(userService.modify(u));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/logon")
    public CommonResponse<String> logon(@RequestBody User user) {
        try {
            return CommonResponse.buildAddSuccessResponse(userService.logon(user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/checkPhone")
    public CommonResponse<String> checkPhone(@RequestBody User user) {
        try {
            return CommonResponse.buildGetSuccessResponse(userService.checkPhone(user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/randomNum")
    public CommonResponse<String> randomNum(@RequestBody User user) {
        try {
            return CommonResponse.buildGetSuccessResponse(userService.randomNum(user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/getWxopenId")
    public CommonResponse<String> getWxopenId(@RequestBody User user) {
        try {
            return CommonResponse.buildAddSuccessResponse(commService.getWxOpenId(user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-user/getUnpayBillModel/{tenatId}")
    public CommonResponse<String> getUnpayBillModel(@PathVariable Long tenatId) {
        try {
            return CommonResponse.buildGetSuccessResponse(payBillService.getUnpayBillModel(tenatId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
